home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / htauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  5.8 KB  |  209 lines

  1.  
  2. /* MODULE                            HTAuth.c
  3. **            USER AUTHENTICATION
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **
  8. ** HISTORY:
  9. **    AL 14.10.93 Fixed the colon-not-allowed-in-password-bug.
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15.  
  16. #include <string.h>
  17.  
  18. #include "HTUtils.h"
  19. #include "HTPasswd.h"    /* Password file routines    */
  20. #include "HTAssoc.h"
  21. #include "HTAuth.h"    /* Implemented here        */
  22. #include "HTUU.h"    /* Uuencoding and uudecoding    */
  23.  
  24.  
  25. /* PRIVATE                        decompose_auth_string()
  26. **        DECOMPOSE AUTHENTICATION STRING
  27. **        FOR BASIC OR PUBKEY SCHEME
  28. ** ON ENTRY:
  29. **    authstring    is the authorization string received
  30. **            from browser.
  31. **
  32. ** ON EXIT:
  33. **    returns        a node representing the user information
  34. **            (as always, this is automatically freed
  35. **            by AA package).
  36. */
  37. PRIVATE HTAAUser *decompose_auth_string ARGS2(char *,        authstring,
  38.                           HTAAScheme,    scheme)
  39. {
  40.     static HTAAUser *user = NULL;
  41.     static char *cleartext = NULL;
  42.     char *username = NULL;
  43.     char *password = NULL;
  44.     char *inet_addr = NULL;
  45.     char *timestamp = NULL;
  46.     char *browsers_key = NULL;
  47.  
  48.     if (!user && !(user = (HTAAUser*)malloc(sizeof(HTAAUser))))    /* Allocated */
  49.     outofmem(__FILE__, "decompose_auth_string");        /* only once */
  50.  
  51.     user->scheme = scheme;
  52.     user->username = NULL;    /* Not freed, because freeing */
  53.     user->password = NULL;    /* cleartext also frees these */
  54.     user->inet_addr = NULL;    /* See below: ||              */
  55.     user->timestamp = NULL;    /*            ||              */
  56.     user->secret_key = NULL;    /*            ||              */
  57.                                 /*            \/              */
  58.     FREE(cleartext);    /* From previous call.                */
  59.                         /* NOTE: parts of this memory are pointed to by    */
  60.                         /* pointers in HTAAUser structure. Therefore,    */
  61.                         /* this also frees all the strings pointed to    */
  62.             /* by the static 'user'.            */
  63.  
  64.     if (!authstring || !*authstring || 
  65.     scheme != HTAA_BASIC || scheme == HTAA_PUBKEY)
  66.     return NULL;
  67.  
  68.     if (scheme == HTAA_PUBKEY) {    /* Decrypt authentication string */
  69.     int bytes_decoded;
  70.     char *ciphertext;
  71.     int len = strlen(authstring) + 1;
  72.  
  73.     if (!(ciphertext = (char*)malloc(len)) ||
  74.         !(cleartext  = (char*)malloc(len)))
  75.         outofmem(__FILE__, "decompose_auth_string");
  76.  
  77.     bytes_decoded = HTUU_decode(authstring, ciphertext, len);
  78.     ciphertext[bytes_decoded] = (char)0;
  79. #ifdef PUBKEY
  80.     HTPK_decrypt(ciphertext, cleartext, private_key);
  81. #endif
  82.     free(ciphertext);
  83.     }
  84.     else {   /* Just uudecode */
  85.     int bytes_decoded;
  86.     int len = strlen(authstring) + 1;
  87.     
  88.     if (!(cleartext = (char*)malloc(len)))
  89.         outofmem(__FILE__, "decompose_auth_string");
  90.     bytes_decoded = HTUU_decode(authstring, cleartext, len);
  91.     cleartext[bytes_decoded] = (char)0;
  92.     }
  93.  
  94.  
  95. /*
  96. ** Extract username and password (for both schemes)
  97. */
  98.     username = cleartext;
  99.     if (!(password = strchr(cleartext, ':'))) {
  100.     if (TRACE)
  101.         fprintf(stderr, "%s %s\n",
  102.             "decompose_auth_string: password field",
  103.             "missing in authentication string.\n");
  104.     return NULL;
  105.     }
  106.     *(password++) = '\0';
  107.  
  108. /*
  109. ** Extract rest of the fields
  110. */
  111.     if (scheme == HTAA_PUBKEY) {
  112.     if (                          !(inet_addr   =strchr(password, ':')) || 
  113.         (*(inet_addr++)   ='\0'), !(timestamp   =strchr(inet_addr,':')) ||
  114.         (*(timestamp++)   ='\0'), !(browsers_key=strchr(timestamp,':')) ||
  115.         (*(browsers_key++)='\0')) {
  116.  
  117.         if (TRACE) fprintf(stderr, "%s %s\n",
  118.                    "decompose_auth_string: Pubkey scheme",
  119.                    "fields missing in authentication string");
  120.         return NULL;
  121.     }
  122.     }
  123.  
  124. /*
  125. ** Set the fields into the result
  126. */
  127.     user->username   = username;
  128.     user->password   = password;
  129.     user->inet_addr  = inet_addr;
  130.     user->timestamp  = timestamp;
  131.     user->secret_key = browsers_key;
  132.  
  133.     if (TRACE) {
  134.     if (scheme==HTAA_BASIC)
  135.         fprintf(stderr, "decompose_auth_string: %s (%s,%s)\n",
  136.             "Basic scheme authentication string:",
  137.             username, password);
  138.     else
  139.         fprintf(stderr, "decompose_auth_string: %s (%s,%s,%s,%s,%s)\n",
  140.             "Pubkey scheme authentication string:",
  141.             username, password, inet_addr, timestamp, browsers_key);
  142.     }
  143.     
  144.     return user;
  145. }
  146.  
  147.  
  148.  
  149. PRIVATE BOOL HTAA_checkTimeStamp ARGS1(CONST char *, timestamp)
  150. {
  151.     return NO;        /* This is just a stub */
  152. }
  153.  
  154.  
  155. PRIVATE BOOL HTAA_checkInetAddress ARGS1(CONST char *, inet_addr)
  156. {
  157.     return NO;        /* This is just a stub */
  158. }
  159.  
  160.  
  161. /* SERVER PUBLIC                    HTAA_authenticate()
  162. **            AUTHENTICATE USER
  163. ** ON ENTRY:
  164. **    scheme        used authentication scheme.
  165. **    scheme_specifics the scheme specific parameters
  166. **            (authentication string for Basic and
  167. **            Pubkey schemes).
  168. **    prot        is the protection information structure
  169. **            for the file.
  170. **
  171. ** ON EXIT:
  172. **    returns        NULL, if authentication failed.
  173. **            Otherwise a pointer to a structure
  174. **            representing authenticated user,
  175. **            which should not be freed.
  176. */
  177. PUBLIC HTAAUser *HTAA_authenticate ARGS3(HTAAScheme,    scheme,
  178.                      char *,    scheme_specifics,
  179.                      HTAAProt *,    prot)
  180. {
  181.     if (HTAA_UNKNOWN == scheme || !prot ||
  182.     -1 == HTList_indexOf(prot->valid_schemes, (void*)scheme))
  183.     return NULL;
  184.  
  185.     switch (scheme) {
  186.       case HTAA_BASIC:
  187.       case HTAA_PUBKEY:
  188.     {
  189.         HTAAUser *user = decompose_auth_string(scheme_specifics, scheme);
  190.                                        /* Remember, user is auto-freed */
  191.         if (user &&
  192.         HTAA_checkPassword(user->username,
  193.                    user->password,
  194.                    HTAssocList_lookup(prot->values, "passw")) &&
  195.         (HTAA_BASIC == scheme ||
  196.          (HTAA_checkTimeStamp(user->timestamp) &&
  197.           HTAA_checkInetAddress(user->inet_addr))))
  198.         return user;
  199.         else
  200.         return NULL;
  201.     }
  202.     break;
  203.       default:
  204.     /* Other authentication routines go here */
  205.     return NULL;
  206.     }
  207. }
  208.  
  209.